<?php

/**
 * Created by Allen.
 * Date: 15-10-12
 * 会员模型
 */

class User_yeji_model extends HS_Model
{

    //获取结果集
    public function getList($where = array(), $limit = '20', $offset = '0', $order_by = 'id desc')
    {
        if (!empty($where['tuandui'])) {
            $wheres = array();
            if (!empty($where['user.username'])) {
                $wheres[] = 'user.username = "' . $where['user.username'] . '"';
            }
            if (!empty($where['user.user_level'])) {
                $wheres[] = 'user.user_level = "' . $where['user.user_level'] . '"';
            }
            if (!empty($where['user_yeji.day >='])) {
                $wheres[] = 'user_yeji.day >= "' . $where['user_yeji.day >='] . '"';
            }
            if (!empty($where['user_yeji.day <='])) {
                $wheres[] = 'user_yeji.day <= "' . $where['user_yeji.day <='] . '"';
            }
            $where_raw = implode(' and ', $wheres);
            $sql = 'SELECT
            user_level,true_name,username,id,user_id, zhitui, tuandui, tuandui_num , zhitui_num,update_time,day
        FROM
            (
             SELECT
             user.user_level,user.true_name,user.username,user_yeji.id,user_yeji.user_id,sum(zhitui) as zhitui,sum(tuandui) as tuandui,sum(tuandui_num) tuandui_num ,sum(zhitui_num) zhitui_num,user_yeji.update_time,user_yeji.day
            FROM
                `user_yeji`
                JOIN `user` ON `user`.`id` = `user_yeji`.`user_id`
            WHERE
            ' . $where_raw . '  GROUP BY `user_yeji`.`user_id` ) AS a WHERE a.tuandui >= ' . $where['tuandui'].' limit '.$offset.','.$limit;
            $re = $this->db->query($sql);

            $re = $re->result_array($re);
            return $re;
        }
        if (isset($where['tuandui'])) {
            unset($where['tuandui']);
        }
        if (!empty($where['user_id in'])) {
            $user_ids = $where['user_id in'];
            unset($where['user_id in']);
            return $this->select('user.user_level,user.true_name,user.username,user_yeji.id,user_yeji.user_id,sum(zhitui) as zhitui,sum(tuandui) as tuandui,sum(tuandui_num) tuandui_num ,sum(zhitui_num) zhitui_num,user_yeji.update_time,user_yeji.day')
                ->join('user', 'user.id = user_yeji.user_id')
                ->where_in('user_yeji.user_id', $user_ids)->where($where)->group_by('user_yeji.user_id')->limit($limit, $offset)->order_by($order_by)->find_all();
        }
        return $this->select('user.user_level,user.true_name,user.username,user_yeji.id,user_yeji.user_id,sum(zhitui) as zhitui,sum(tuandui) as tuandui,sum(tuandui_num) tuandui_num ,sum(zhitui_num) zhitui_num,user_yeji.update_time,user_yeji.day')
            ->join('user', 'user.id = user_yeji.user_id')
            ->where($where)->group_by('user_yeji.user_id')->limit($limit, $offset)->order_by($order_by)->find_all();
    }
    //获取结果集
    public function getListByUser($where = array(), $limit = '20', $offset = '0', $order_by = 'id desc')
    {
        return $this->where($where)->limit($limit, $offset)->order_by($order_by)->find_all();
    }
    //获取结果集
    public function getListAll($where = array(), $limit = '20', $offset = '0', $order_by = 'id desc')
    {
        return $this->where($where)->order_by($order_by)->find_all();
    }

    //计算行数
    public function getCount($where = array())
    {
        if (!empty($where['tuandui'])) {
            $wheres = array();
            if (!empty($where['user.username'])) {
                $wheres[] = 'user.username = "' . $where['user.username'] . '"';
            }
            if (!empty($where['user.user_level'])) {
                $wheres[] = 'user.user_level = "' . $where['user.user_level'] . '"';
            }
            if (!empty($where['user_yeji.day >='])) {
                $wheres[] = 'user_yeji.day >= "' . $where['user_yeji.day >='] . '"';
            }
            if (!empty($where['user_yeji.day <='])) {
                $wheres[] = 'user_yeji.day <= "' . $where['user_yeji.day <='] . '"';
            }
            $where_raw = implode(' and ', $wheres);
            $sql = 'SELECT
            count(*) as num
        FROM
            (
             SELECT
                sum( tuandui ) AS tuandui
            FROM
                `user_yeji`
                JOIN `user` ON `user`.`id` = `user_yeji`.`user_id`
            WHERE
            ' . $where_raw . '  GROUP BY `user_yeji`.`user_id` ) AS a WHERE a.tuandui >= ' . $where['tuandui'];
            $re = $this->db->query($sql);

            $re = $re->result_array($re);
            return isset($re[0]['num']) ? $re[0]['num'] : 0;
        }
        if (isset($where['tuandui'])) {
            unset($where['tuandui']);
        }
        if (!empty($where['user_id in'])) {
            $user_ids = $where['user_id in'];
            unset($where['user_id in']);
            return $this->join('user', 'user.id = user_yeji.user_id')->where_in('user_yeji.user_id', $user_ids)->where($where)->group_by('user_id')->count();
        }

        return $this->join('user', 'user.id = user_yeji.user_id')->where($where)->group_by('user_yeji.user_id')->count();
    }

    //获取单个
    public function getOne($where)
    {
        return $this->where($where)->find();
    }

    //删除
    public function del($where)
    {
        return $this->where($where)->delete();
    }

    //编辑
    public function update($where, $data)
    {
        return $this->where($where)->edit($data);
    }

    //添加
    public function save($data)
    {
        if ($this->add($data)) {
            return $this->db->insert_id();
        }
    }
}
